/*
 * File: Memcache.java Author: Robert Bittle <guywithnose@gmail.com>
 */
package data;

import java.util.Collections;
import net.sf.jsr107cache.Cache;
import net.sf.jsr107cache.CacheException;
import net.sf.jsr107cache.CacheFactory;
import net.sf.jsr107cache.CacheManager;

/**
 * The Class Memcache.
 * 
 * @author Dude
 */
public class Memcache
{

    /** The cache. */
    private static Cache cache;

    /**
     * Sets the.
     * 
     * @param key
     *            the key
     * @param value
     *            the value
     */
    public static void set(String key, String value)
    {
        if (cache == null)
            loadCache();
        cache.put(key, value);
    }

    /**
     * Gets the.
     * 
     * @param key
     *            the key
     * @return the string
     */
    public static String get(String key)
    {
        if (cache == null)
            loadCache();
        return (String) cache.get(key);
    }

    /**
     * Clear.
     */
    public static void clear()
    {
        if (cache == null)
            loadCache();
        cache.clear();
    }

    /**
     * Load cache.
     */
    private static void loadCache()
    {
        try
        {
            CacheFactory cacheFactory = CacheManager.getInstance()
                    .getCacheFactory();
            cache = cacheFactory.createCache(Collections.emptyMap());
        } catch (CacheException e)
        {
            e.printStackTrace();
        }
    }

}
